import { Redirect, Route, RouteProps } from 'react-router-dom'
import { hasToken } from '../../utils/storage'

// vue 百度
// 1 react 谷歌
// 2 源码
// 3 积累
interface Props extends RouteProps {
  // 组件的props可以接受任意类型
  component: React.ComponentType<any>
}
export default function index({ component: Component, ...rest }: Props) {
  return (
    <Route
      {...rest}
      render={({ location }) => {
        if (hasToken()) {
          console.log(hasToken())
          return <Component></Component>
        } else {
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: location }, //这里加了个state
              }}
            ></Redirect>
          )
        }
      }}
    ></Route>
  )
}
